package com.example.communicationfragment.activity;

import android.os.Bundle;
import android.widget.Button;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.example.communicationfragment.R;
import com.example.communicationfragment.fragment.FragmentWithInterface;
import com.example.communicationfragment.linster.LinterDataChanged;

/**
 * 用接口实现 activity和fragment之间通信
 */
public class Main_Interface_Activity extends AppCompatActivity {

    private LinterDataChanged dataChanged;
    private static int count = 0;

    public void setDataChanged(LinterDataChanged dataChanged) {
        this.dataChanged = dataChanged;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        FragmentWithInterface fragment = new FragmentWithInterface();
        setDataChanged(fragment);

        Button sendButton = findViewById(R.id.send_button);
        sendButton.setOnClickListener(v -> {
            if (dataChanged != null) {
                dataChanged.onDataChanged("" + ++count);
            }
        });

    }
}
